using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CSSg2Help
{
    public class Postion
    {
        public int x;
        public int y;
        public Postion(int x,int y)
        {
            this.x = x;
            this.y = y;
        }
        ~Postion() { }
    }

    public class Trace
    {
        public string name = "";
        public List<Postion> posList = new List<Postion>();
        public Trace(string str) 
        {
            string[] substr = str.Split('=');
            if (substr.Length != 2)
            {
                return;
            }
            this.name = substr[0];
            string[] pos = substr[1].Split(';');
            for (int i = 0; i < pos.Length; i++)
            {
                string[] xy = pos[i].Split(',');
                if (xy.Length != 2)
                {
                    continue;
                }
                int x = Convert.ToInt32(xy[0]);
                int y = Convert.ToInt32(xy[1]);
                Postion p = new Postion(x, y);
                posList.Add(p);
            }
        }
        ~Trace() { }
        public void AddPostion(Postion pos)
        {
            posList.Add(pos);
        }
    }
    public class TraceList
    {
        public List<Trace> traceList = new List<Trace>();
        public string filename;
        public TraceList(string filename)
        {
            this.filename = filename;
            if (!File.Exists(filename))
            {
                return;
            }
            StreamReader sr = File.OpenText(filename);
            string trace = "";
            while ((trace = sr.ReadLine()) != null)
            {
                Trace t = new Trace(trace);
                traceList.Add(t);
            }
            sr.Close();
        }
        ~TraceList() { }
        public void Save(int index)
        {
            string trace = "";
            for (int i=0;i<this.traceList[index].posList.Count;i++)
            {
                trace += this.traceList[index].posList[i].x + "," + this.traceList[index].posList[i].y + ";";
            }
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            StringBuilder sb = new StringBuilder();
            string line=sr.ReadLine();
            for (int i = 0; i < this.traceList.Count; i++)
            {
                string name = line.Split('=')[0];
                if (name == this.traceList[index].name)
                {
                    sb.Append(name + "=" + trace + "\r\n");
                }
                else
                {
                    sb.Append(line + "\r\n");
                }
                line = sr.ReadLine();
            }
            sr.Close();
            fs.Close();
            FileStream fs1 = new FileStream(filename, FileMode.Open, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs1);
            sw.Write(sb.ToString());
            sw.Close();
            fs1.Close();
        }
    }
}
